<?php

use Illuminate\Database\Seeder;
use App\Models\User;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);

        $users = factory(User::class)
                        ->times(100)
                        ->make()->each(function($user, $index) {
                            if (rand(1,2) === 2 && $user->id < 3) $user->parent_id = 3;
                            else $user->parent_id = 2;
                        });

        // 让隐藏字段可见，并将数据集合转换为数组
        $user_array = $users->makeVisible(['password', 'remember_token'])->toArray();

        // 插入到数据库中
        User::insert($user_array);

        // 单独处理第一个用户的数据
        $user = User::find(1);
        $user->name = 'admin';
        $user->email = 'admin@qq.com';
        $user->parent_id = 0;
        $user->save();

        $user->assignRole('Founder');

        $user = User::find(2);
        $user->name = '测试代理1';
        $user->email = 'daili@qq.com';
        $user->parent_id = 0;
        $user->save();
        $user->assignRole('Daili');

        $user = User::find(3);
        $user->name = '测试代理2';
        $user->email = 'fengzi93@vip.qq.com';
        $user->parent_id = 0;
        $user->save();
        $user->assignRole('Daili');
    }
}
